﻿(function()
{
    /*################################命名空间开始###########################*/
    /*============================全局变量定义区开始=========================*/
    //次范围内定义的变量与外部同名变量不冲突
    var flashObj = null;
    var ReciveQueue = new Array();
    var ComPackge = null; //与后台通信数数据包对象    
    /*============================全局变量定义区结束=========================*/
    if (window.XJNet == null)
    {
        /*****************************XJNet库开始***************************/
        window.XJNet =
        {
            /*----------------------------属性定义区开始-------------------------*/
            ConnectFlag: false,
            RegisterFlag: false,
            IsWriteLog: false,
            IsAlertLog: false,
            SwfContainerID: "SwfContent",
            RegisterInfo: { ISREGISTER: false, CONTENT: "" },
            //IP信息
            IPEndPoint: { WebDppIp: "", WebDppPort: "" },
            //flash对象变量
            Flashvars: { name1: "hello", name2: "world", name3: "foobar" },
            LogctrlID: "", //用于接收日志的文本容器控件ID
            LogctrlDebugID: "",
            //flash对象参数
            FlashParams: {
                menu: "false",
                //movie:"SocketComponet.swf",
                quality: "high",
                bgcolor: "#869ca7",
                allowScriptAccess: "sameDomain"
            },
            //flash对象属性，很重要
            FlashAttributes:
            {
                id: "SocketComponent",
                name: "SocketComponent"
            },
            /*----------------------------属性定义区结束-------------------------*/
            /*----------------------------事件定义区开始-------------------------*/
            //这里可以定义一些事件，需要在调用前注册（例如XJNet.OnConnected = function(){alert('连接成功完成后会弹出此信息！');}）
            OnLoad: null,
            OnConnecting: null,
            OnConnected: null,
            OnReceive: null,
            /*----------------------------事件定义区结束-------------------------*/
            /*----------------------------方法定义区开始-------------------------*/
            //SWF组建状态输出函数
            MsgLog: function(msg)
            {
                if (XJNet.IsWriteLog)
                {
                    try
                    {
                        var log = document.getElementById(XJNet.LogctrlID.toString());
                        if (log)
                        {
                            log.value += msg + "\n";
                        }
                    }
                    catch (e) { alert('获得日志输出容器控件失败！'); }
                }
            },
            DebugLog: function(msg)
            {
                if (XJNet.IsWriteLog)
                {
                    try
                    {
                        var log = document.getElementById(XJNet.LogctrlDebugID.toString());
                        if (log)
                        {
                            log.value += msg + "\n";
                        }
                    }
                    catch (e) { alert('获得日志输出容器控件失败！'); }
                }
            },
            //初始化SWF对象
            InitSwfObject: function()
            {
                if (flashObj == "undefined" || flashObj == null)
                {
                    try
                    {
                        //                        if (!document.getElementById("SwfContent"))
                        //                        {
                        //                            var DivSwfContent = document.createElement("div");
                        //                            DivSwfContent.id = XJNet.SwfContainerID;                            
                        //                            var DivSwfContainer = document.createElement("div");
                        //                            DivSwfContainer.id = "SwfContent";
                        //                            DivSwfContainer.appendChild(DivSwfContent);
                        //                            document.body.appendChild(DivSwfContainer);
                        //                        }
                        flashObj = document.getElementById(XJNet.FlashAttributes.id.toString());
                        XJNet.MsgLog("initialize object name is " + flashObj.name);
                    }
                    catch (e)
                    {
                        MsgLog(e.message);
                    }
                }
            },
            //注册SWF组件
            RegistSwfObject: function(flashUrl, containerid)
            {
                try
                {
                    var t = swfobject.embedSWF(flashUrl, containerid, "0", "0", "9.0.0", "swf/expressInstall.swf", XJNet.Flashvars, XJNet.FlashParams, XJNet.FlashAttributes);
                    return true;
                }
                catch (e)
                {
                    XJNet.MsgLog(e.message);
                    return false;
                }
            },
            Connect: function(IPEndPoint)
            {
                var result = false;
                try
                {
                    XJNet.InitSwfObject();
                    result = flashObj.Connect(IPEndPoint.WebDppIp, parseInt(IPEndPoint.WebDppPort));
                }
                catch (e)
                {
                    XJNet.MsgLog(e.message);
                    throw e;
                }
                return result;
            },
            Connecting: function()//flash组件内部连接前调用此方法
            {
                if (XJNet.OnConnecting)
                {
                    XJNet.OnConnecting(); //触发用户自己实现的连接前的处理事件
                }
            },
            Connected: function()//flash组件内部连接后调用此方法
            {
                //setTimeout(function()
                //{
                if (XJNet.OnConnected)
                {
                    XJNet.OnConnected(); //触发用户自己实现的连接后的处理事件
                }
                //}, 4000);
            },
            Send: function(jsonstr)
            {
                var result = false;
                if (XJNet.ConnectFlag)
                {
                    try
                    {
                        result = flashObj.Send(jsonstr);
                    }
                    catch (e)
                    {
                        result = false;
                        XJNet.MsgLog(e.message);
                    }
                }
                else
                {
                    result = false;
                    XJNet.MsgLog("连接尚未建立！");
                }
                return result;
            },
            //接受WEBDPP推送的JSON串
            Receive: function(jsonstr)
            {
                if (XJNet.IsWriteLog)
                {
                    XJNet.MsgLog(jsonstr);
                }
                if (XJNet.OnReceive)
                {
                    XJNet.OnReceive(jsonstr);
                }
            },
            Load: function()
            {
                debugger;
                XJNet.InitSwfObject();
                flashObj.LoadPolicyFile(XJNet.IPEndPoint.WebDppIp, XJNet.IPEndPoint.WebDppPort);
                XJNet.ConnectFlag = XJNet.Connect(XJNet.IPEndPoint);
                if (XJNet.OnLoad)
                    XJNet.OnLoad();
            },
            Close: function()
            {
                flashObj.Close();
            }
            /*----------------------------方法定义区结束-------------------------*/
        };
        /*****************************XJNet库结束***************************/
    }
    /*################################命名空间结束###########################*/
})();
